import axios from "axios";

const BASE_URL = "https://api.telegramloveai.com/restApi/posts";

const api = axios.create({
  baseURL: BASE_URL,
  headers: {
    "Content-Type": "application/json",
    Authorization:
      "Bearer eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiJqbnU3OTEiLCJzdWIiOiIxNjQwNTQzOTY0Njc3MzIwNzA2IiwiZXhwIjoxNjgyMzkwMzM1LCJpYXQiOjE2Nzk5NzExMzV9.C58hQ903EPbRN8Xo_Vdrml9lQiiahdR_YVYbWL9osoxRfr9QlZq89mpuy-GnoVkiEEntgLt7XC5-yxHUXlbzVQ",
  },
});

// Add response interceptor to handle common response structure
api.interceptors.response.use(
  (response) => {
    if (response.data.success === false) {
      return Promise.reject(new Error(response.data.message || "API Error"));
    }
    return response;
  },
  (error) => {
    const message =
      error.response?.data?.message || error.message || "Network error";
    return Promise.reject(new Error(message));
  }
);

export const getPosts = async (pageNo, pageSize) => {
  try {
    const response = await api.post(`/list/${pageNo}/${pageSize}`);
    return response.data.result; // Return the entire result object which contains records and pagination info
  } catch (error) {
    throw new Error(`Failed to fetch posts: ${error.message}`);
  }
};

export const createPost = async (postData) => {
  try {
    // Transform the data to match API expectations
    const transformedData = {
      ...postData,
      image_url1: postData.imageUrls[0] || "",
      image_url2: postData.imageUrls[1] || "",
      image_url3: postData.imageUrls[2] || "",
      image_url4: postData.imageUrls[3] || "",
      image_url5: postData.imageUrls[4] || "",
      image_count: postData.imageUrls.length,
      friend_style_sn: postData.friendStyleSn,
    };

    const response = await api.post("/create", transformedData);
    return response.data;
  } catch (error) {
    throw new Error(`Failed to create post: ${error.message}`);
  }
};

export const editPost = async (postId, postData) => {
  try {
    // Transform the data to match API expectations
    const transformedData = {
      ...postData,
      image_url1: postData.imageUrls[0] || "",
      image_url2: postData.imageUrls[1] || "",
      image_url3: postData.imageUrls[2] || "",
      image_url4: postData.imageUrls[3] || "",
      image_url5: postData.imageUrls[4] || "",
      image_count: postData.imageUrls.length,
      friend_style_sn: postData.friendStyleSn,
    };

    const response = await api.post(`/edit/${postId}`, transformedData);
    return response.data;
  } catch (error) {
    throw new Error(`Failed to update post: ${error.message}`);
  }
};

export const deletePost = async (postId) => {
  try {
    const response = await api.post(`/delete/${postId}`);
    return response.data;
  } catch (error) {
    throw new Error(`Failed to delete post: ${error.message}`);
  }
};

export const getFriendStyles = async () => {
  try {
    const response = await api.get("/friendStyles");
    return response.data;
  } catch (error) {
    throw new Error(`Failed to fetch friend styles: ${error.message}`);
  }
};

export default api;
